//
//  ManageStrutureModel.m
//  MobileOffice
//
//  Created by jjkj on 16/12/7.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "ManageStrutureModel.h"

@implementation ManageStrutureModel
- (instancetype)initWithResponse:(id)responseObject{
    if (self=[super init]) {
        if ([responseObject[@"code"] isKindOfClass:[NSNumber class]]) {
            NSNumber *num =responseObject[@"code"] ;
            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
            
            NSString* str = [numberFormatter stringFromNumber:num];
            
            self.code=str?str:@"";
        }else{
            self.code=responseObject[@"code"]?responseObject[@"code"]:@"";
        }
        
        self.msg=responseObject[@"msg"]?responseObject[@"msg"]:@"";
        
        NSDictionary *dataDict=responseObject[@"retContent"];
        [self setValuesForKeysWithDictionary:dataDict];
        NSArray *list=dataDict[@"manageStructureList"];
        _dataArr=[NSMutableArray new];
        _dataDict=[NSMutableDictionary new];
        NSMutableArray *arr=[NSMutableArray new];
        for (NSDictionary *dict in list) {
            ManageStrutureModel *model=[ManageStrutureModel new];
            [model setValuesForKeysWithDictionary:dict];
            //先找到二级部门
            if (model.parentItemId==nil||model.parentItemId.length==0) {
                [_dataArr addObject:model];
            }else{
                [arr addObject:model];
            }
        }
        //把所有父id一样的 放到一起
        for (ManageStrutureModel *model in arr) {
            NSMutableArray *manageArr=[_dataDict objectForKey:model.parentItemId];
            if (manageArr==nil) {
                manageArr=[NSMutableArray new];
                [_dataDict setObject:manageArr forKey:model.parentItemId];
            }
            [manageArr addObject:model];
        }

        
//        self.dataDict=[NSMutableDictionary new];
//        for (ManageStrutureModel *model in _dataArr) {
//            if (model.parentItemId.length==0) {
//                if (![self.dataDict.allKeys containsObject:model.itemValue]) {
//                    NSMutableDictionary *parents=[NSMutableDictionary new];
//                    [self.dataDict setObject:parents forKey:model.itemId];
//                }
//            }
//            
//        }
//        
    }
    
    return self;
}

@end
